package com.example.demo.controller;

import com.example.demo.domain.Product;
import com.example.demo.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by zhangjiawen on 2019/5/17.
 */
@RestController
@RequestMapping("/api/v1/product")
@RefreshScope
public class ProductController {
    @Autowired
    private ProductService productService;
    @Value("${server.port}")
    private  int port;
    @Value("${env}")
    private String env;

    @RequestMapping("getList")
    public List<Product> getList(HttpServletRequest request){
        String token = request.getHeader("token");
        String cookie = request.getHeader("Cookie");
        System.out.println(token);
        System.out.println(cookie);
        return productService.getProductList();
    }

    @RequestMapping("getById")
    public Product getById(int id){
        //模拟接口响应慢，线程睡眠新的方式
//        try {
//            TimeUnit.SECONDS.sleep(1);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        Product product = productService.getProductById(id);
        Product p2=new Product();
        BeanUtils.copyProperties(product,p2);
        p2.setName(product.getName()+"data from port="+port+"env="+env);
        return p2;
    }
}
